//
//  OneKindWineVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/6.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "OneKindWineVC.h"
#import "WineListCell.h"
#import "OneGoodsModel.h"
#import "GoodDetailController.h"
@interface OneKindWineVC ()

@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, strong) NSString *hasmore;


@property (nonatomic, strong) NSMutableArray<OneGoodsModel *> *dataModels;
@end

@implementation OneKindWineVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.plainTableView.rowHeight = 96;
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([WineListCell class]) bundle:nil] forCellReuseIdentifier:[WineListCell identifierStr]];
    
    
    
    MJWeakSelf
    self.plainTableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [weakSelf.plainTableView.mj_header beginRefreshing];
    
    

        
        
    self.plainTableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
            
            [weakSelf loadMoreData];
            
        }];
    
    
    CGFloat navH = IPhoneX ? 88 : 64;
    CGFloat tabbarH = IPhoneX ? 83 : 49;
    
    if (@available(iOS 11.0, *)) {
        
        self.plainTableView.contentInset = UIEdgeInsetsMake(XMGTitlesViewH, 0, tabbarH, 0);
       
    }else{
        
        self.plainTableView.contentInset = UIEdgeInsetsMake(navH + XMGTitlesViewH, 0, tabbarH,0 );

        
    }

   
    
}


- (void)loadData{
    self.curpage = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"gc_id"] = @"633";
    parameters[@"curpage"] = @"1";
    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/plain", @"text/javascript",@"text/html" ,nil];
    
    
    
    [manger GET:@"http://mall.xjscvip.com/mobile/index.php?c=goods&a=goods_list" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        NSLog(@"%@",responseObject);
        
        
        self.hasmore = responseObject[@"data"][@"has_more"];
        
        self.dataModels = [OneGoodsModel mj_objectArrayWithKeyValuesArray:responseObject[@"datas"][@"goods_list"]];
        
        [self.plainTableView.mj_footer resetNoMoreData];
        
        if ([self.hasmore isEqualToString:@"1"]) {
            
        } else {
            
            if (self.dataModels.count > 0) {
                
                [self.plainTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                [SVProgressHUD showInfoWithStatus:@"暂无此类商品!"];
                
            }
        }
        
        [self.plainTableView.mj_header endRefreshing];
        
        [self.plainTableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.plainTableView.mj_header endRefreshing];
        
//        NSLog(@"%@",error);
        
        [SVProgressHUD showInfoWithStatus:error.domain];
    }];
    
    
    return;
    
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"mobile/index.php?c=goods&a=goods_list" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
//            NSLog(@"%@",[responseObject mj_JSONString]);
            
            
            self.hasmore = responseObject[@"data"][@"has_more"];
            
            self.dataModels = [OneGoodsModel mj_objectArrayWithKeyValuesArray:responseObject[@"datas"][@"goods_list"]];
            
            [self.plainTableView.mj_footer resetNoMoreData];
            
            if ([self.hasmore isEqualToString:@"1"]) {
                
            } else {
                
                if (self.dataModels.count > 0) {
                    
                    [self.plainTableView.mj_footer endRefreshingWithNoMoreData];
                    
                } else {
                    
                    [SVProgressHUD showInfoWithStatus:@"暂无此类商品!"];
                    
                }
            }
            
            [self.plainTableView.mj_header endRefreshing];
            
            [self.plainTableView reloadData];
            
            
        }else{
            
            [self.plainTableView.mj_header endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
        }
        
    }];
    
}



- (void)loadMoreData{
    
     self.curpage ++;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"gc_id"] = @"633";
    parameters[@"curpage"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];

    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"mobile/index.php?c=goods&a=goods_list" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            self.hasmore = responseObject[@"data"][@"has_more"];
            
            [self.dataModels addObjectsFromArray:[OneGoodsModel mj_objectArrayWithKeyValuesArray:responseObject[@"datas"][@"goods_list"]]];
            
            
            if ([self.hasmore isEqualToString:@"1"]) {
                
                [self.plainTableView.mj_footer endRefreshing];
                
            } else {
                
                
                
                [self.plainTableView.mj_footer endRefreshingWithNoMoreData];
                
                
            }
            
            
            [self.plainTableView reloadData];
            
        }else{
            
            [self.plainTableView.mj_footer endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
    }];
    
}



#pragma mark - tableviewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.dataModels == nil) {
        return  0;
    } else {
        return  1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataModels == nil) {
        return  0;
    } else {
        return  self.dataModels.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WineListCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[WineListCell identifierStr]];
    
    tableCell.model = self.dataModels[indexPath.row];
   
    return tableCell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GoodDetailController *tempVC = [[GoodDetailController alloc]init];
    
    tempVC.goodsModel = self.dataModels[indexPath.row];
    
    [self.navigationController pushViewController:tempVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
